﻿
namespace ATA.Windows.Forms
{
	/// <summary>
	/// abstract DaiLy typed datagridview
	/// </summary>
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	public abstract class DaiLyDataGridViewBase : System.Windows.Forms.UserControl
	{	
		/// <summary> 
		/// Thrown when the current Entity changed.
		/// </summary>
		public event System.EventHandler<DaiLyDataGridViewEventArgs> CurrentEntityChanged;
		
		#region Fields
				
		/// <summary> 
		/// the current entity.
		/// </summary>
		private Entities.DaiLy _currentDaiLy = null;
		//private Dictionary<string, string> _columnsHeaders = new Dictionary<string, string>();
		
		/// <summary> 
		/// the datagridview instance
		/// </summary>
		protected System.Windows.Forms.DataGridView uxDaiLyDataGridView;
		
		/// <summary> 
		/// the errorprovider
		/// </summary>
		protected System.Windows.Forms.ErrorProvider uxDaiLyErrorProvider;
		
		/// <summary> 
		/// the bindingsource
		/// </summary>
		protected System.Windows.Forms.BindingSource uxDaiLyBindingSource;
		
		
		/// <summary> 
		/// the DGV column associated with the DaiLyId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxDaiLyIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the TenDaiLy property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxTenDaiLyDataGridViewColumn;
		/// <summary> 
		/// the DGV column associated with the LoaiDaiLyId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewComboBoxColumn uxLoaiDaiLyIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the ParentId property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxParentIdDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the LastUpdate property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxLastUpdateDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the UpdateBy property
		/// </summary>
		protected System.Windows.Forms.DataGridViewTextBoxColumn uxUpdateByDataGridViewColumn;
		
		/// <summary> 
		/// the DGV column associated with the IsActive property
		/// </summary>
		protected System.Windows.Forms.DataGridViewCheckBoxColumn uxIsActiveDataGridViewColumn;
		
		#endregion
					
		#region Combo list
		
				
		private Entities.TList<Entities.LoaiKhachHang> _LoaiDaiLyIdList;
		
		/// <summary> 
		/// The list of selectable LoaiKhachHang
		/// </summary>
		[System.ComponentModel.Category("DropDownList BindingSources")]
		public Entities.TList<Entities.LoaiKhachHang> LoaiDaiLyIdList
		{
			get {return this._LoaiDaiLyIdList;}
			set 
			{
				this._LoaiDaiLyIdList = value;
				this.uxLoaiDaiLyIdDataGridViewColumn.DataSource = null;
				this.uxLoaiDaiLyIdDataGridViewColumn.DataSource = this._LoaiDaiLyIdList;
			}
		}
		
		private bool _allowNewItemInLoaiDaiLyIdList;
		
		/// <summary> 
		/// Indicates if user can add an item in the list of LoaiKhachHang
		/// </summary>
		[System.ComponentModel.Category(".netTiers"), System.ComponentModel.Description("Indicates if end user can add a new item in the combobox. an event will be thrown then.")]
		public bool AllowNewItemInLoaiDaiLyIdList
		{
			get { return _allowNewItemInLoaiDaiLyIdList;}
			set
			{
				this._allowNewItemInLoaiDaiLyIdList = value;
				this.uxLoaiDaiLyIdDataGridViewColumn.DisplayStyle = value ? System.Windows.Forms.DataGridViewComboBoxDisplayStyle.ComboBox : System.Windows.Forms.DataGridViewComboBoxDisplayStyle.DropDownButton;
			}
		}
		
		#endregion
		
		#region Main Datasource
		
		private Entities.TList<Entities.DaiLy> _DaiLyList;
				
		/// <summary> 
		/// The list of DaiLy to be displayed in the DGV.
		/// </summary>
		[System.ComponentModel.Category("Data")]
		public Entities.TList<Entities.DaiLy> DaiLyList
		{
			get {return this._DaiLyList;}
			set
			{
				this._DaiLyList = value;
				this.uxDaiLyBindingSource.DataSource = null;
				this.uxDaiLyBindingSource.DataSource = value;
				this.uxDaiLyDataGridView.DataSource = null;
				this.uxDaiLyDataGridView.DataSource = this.uxDaiLyBindingSource;				
				//this.uxDaiLyBindingSource.ListChanged += new System.ComponentModel.ListChangedEventHandler(uxDaiLyBindingSource_ListChanged);
				this.uxDaiLyBindingSource.CurrentItemChanged += new System.EventHandler(OnDaiLyBindingSourceCurrentItemChanged);
			}
		}
		
		private void OnDaiLyBindingSourceCurrentItemChanged(object sender, System.EventArgs e)
		{
			_currentDaiLy = uxDaiLyBindingSource.Current as Entities.DaiLy;
			
			if (_currentDaiLy != null)
			{
				_currentDaiLy.Validate();
			}
			//_DaiLy.Validate();
			OnCurrentEntityChanged();
		}

		//void uxDaiLyBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)
		//{
			//throw new Exception("The method or operation is not implemented.");
		//}		
		
		#endregion
		
		/// <summary>
		/// Gets the currently selected <see cref="Entities.DaiLy"/> instance.
		/// </summary>
		public Entities.DaiLy SelectedDaiLy
		{
			get {return this._currentDaiLy;}
		}
		
		#region other properties
		
		/// <summary>
		/// Gets or sets a value indicating whether you have provided your own data-management operations for the DataGridView control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool VirtualMode
		{
			get {return this.uxDaiLyDataGridView.VirtualMode;}
			set
			{
				this.uxDaiLyDataGridView.VirtualMode = value;
			}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the option to add rows is displayed to the user.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToAddRows
		{
			get {return this.uxDaiLyDataGridView.AllowUserToAddRows;}
			set {this.uxDaiLyDataGridView.AllowUserToAddRows = value;}
		}
		
		/// <summary>
		/// Gets or sets a value indicating whether the user is allowed to delete rows from the DataGridView.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public bool AllowUserToDeleteRows
		{
			get {return this.uxDaiLyDataGridView.AllowUserToDeleteRows;}
			set {this.uxDaiLyDataGridView.AllowUserToDeleteRows = value;}
		}
		
		/// <summary>
		/// Gets the <see cref="DataGridView"/> instance.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]
		public System.Windows.Forms.DataGridView DataGridView
		{
			get {return this.uxDaiLyDataGridView;}
		}
		
		/// <summary>
		/// Gets a collection that contains all the columns in the control.
		/// </summary>
		[System.ComponentModel.Category(".netTiers")]		
		[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Visible)]
		//[System.ComponentModel.EditorAttribute("System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, System.Design", typeof(System.Drawing.Design.UITypeEditor))] 
		public System.Windows.Forms.DataGridViewColumnCollection Columns
		{
			get { return this.uxDaiLyDataGridView.Columns; }
		}
		
		#endregion
		
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;
	
		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (components != null))
			{
				components.Dispose();
			}
			base.Dispose(disposing);
		}
	
		#region Constructor
	
		/// <summary>
		/// Initializes a new instance of the <see cref="DaiLyDataGridViewBase"/> class.
		/// </summary>
		public DaiLyDataGridViewBase()
		{			
			this.components = new System.ComponentModel.Container();			
			
			this.uxDaiLyDataGridView = new System.Windows.Forms.DataGridView();
			this.uxDaiLyBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.uxDaiLyErrorProvider = new System.Windows.Forms.ErrorProvider(this.components);
			this.uxDaiLyIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxTenDaiLyDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxLoaiDaiLyIdDataGridViewColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
			this.uxParentIdDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxLastUpdateDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxUpdateByDataGridViewColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.uxIsActiveDataGridViewColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
			//this.uxLoaiDaiLyIdBindingSource = new LoaiKhachHangBindingSource(this.components);
			//((System.ComponentModel.ISupportInitialize)(this.uxLoaiDaiLyIdBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDaiLyDataGridView)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDaiLyBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDaiLyErrorProvider)).BeginInit();
			this.SuspendLayout();
						
			// 
			// uxDaiLyErrorProvider
			// 
			this.uxDaiLyErrorProvider.ContainerControl = this;
			this.uxDaiLyErrorProvider.DataSource = this.uxDaiLyBindingSource;						
			// 
			// uxDaiLyDataGridView
			// 
			this.uxDaiLyDataGridView.AutoGenerateColumns = false;
			this.uxDaiLyDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.uxDaiLyDataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
		this.uxDaiLyIdDataGridViewColumn,
		this.uxTenDaiLyDataGridViewColumn,
		this.uxLoaiDaiLyIdDataGridViewColumn,
		this.uxParentIdDataGridViewColumn,
		this.uxLastUpdateDataGridViewColumn,
		this.uxUpdateByDataGridViewColumn,
		this.uxIsActiveDataGridViewColumn			});
			this.uxDaiLyDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
			this.uxDaiLyDataGridView.Location = new System.Drawing.Point(0, 0);
			this.uxDaiLyDataGridView.Name = "uxDaiLyDataGridView";
			this.uxDaiLyDataGridView.Size = new System.Drawing.Size(470, 300);
			this.uxDaiLyDataGridView.TabIndex = 0;	
			this.uxDaiLyDataGridView.BackgroundColor = System.Drawing.Color.WhiteSmoke;
			this.uxDaiLyDataGridView.EnableHeadersVisualStyles = false;
			this.uxDaiLyDataGridView.DataError += new System.Windows.Forms.DataGridViewDataErrorEventHandler(this.OnDaiLyDataGridViewDataError);
			this.uxDaiLyDataGridView.CellValueNeeded += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnDaiLyDataGridViewCellValueNeeded);
			this.uxDaiLyDataGridView.CellValuePushed += new System.Windows.Forms.DataGridViewCellValueEventHandler(this.OnDaiLyDataGridViewCellValuePushed);
			
			//
			// uxDaiLyIdDataGridViewColumn
			//
			this.uxDaiLyIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxDaiLyIdDataGridViewColumn.DataPropertyName = "DaiLyId";
			this.uxDaiLyIdDataGridViewColumn.HeaderText = "DaiLyId";
			this.uxDaiLyIdDataGridViewColumn.Name = "uxDaiLyIdDataGridViewColumn";
			this.uxDaiLyIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxDaiLyIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxDaiLyIdDataGridViewColumn.ReadOnly = true;		
			//
			// uxTenDaiLyDataGridViewColumn
			//
			this.uxTenDaiLyDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxTenDaiLyDataGridViewColumn.DataPropertyName = "TenDaiLy";
			this.uxTenDaiLyDataGridViewColumn.HeaderText = "TenDaiLy";
			this.uxTenDaiLyDataGridViewColumn.Name = "uxTenDaiLyDataGridViewColumn";
			this.uxTenDaiLyDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxTenDaiLyDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxTenDaiLyDataGridViewColumn.ReadOnly = false;		
			//
			// uxLoaiDaiLyIdDataGridViewColumn
			//
			this.uxLoaiDaiLyIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxLoaiDaiLyIdDataGridViewColumn.DataPropertyName = "LoaiDaiLyId";
			this.uxLoaiDaiLyIdDataGridViewColumn.HeaderText = "LoaiDaiLyId";
			this.uxLoaiDaiLyIdDataGridViewColumn.Name = "uxLoaiDaiLyIdDataGridViewColumn";
			this.uxLoaiDaiLyIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxLoaiDaiLyIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxLoaiDaiLyIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxParentIdDataGridViewColumn
			//
			this.uxParentIdDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxParentIdDataGridViewColumn.DataPropertyName = "ParentId";
			this.uxParentIdDataGridViewColumn.HeaderText = "ParentId";
			this.uxParentIdDataGridViewColumn.Name = "uxParentIdDataGridViewColumn";
			this.uxParentIdDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxParentIdDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxParentIdDataGridViewColumn.ReadOnly = false;		
			//
			// uxLastUpdateDataGridViewColumn
			//
			this.uxLastUpdateDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxLastUpdateDataGridViewColumn.DataPropertyName = "LastUpdate";
			this.uxLastUpdateDataGridViewColumn.HeaderText = "LastUpdate";
			this.uxLastUpdateDataGridViewColumn.Name = "uxLastUpdateDataGridViewColumn";
			this.uxLastUpdateDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxLastUpdateDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxLastUpdateDataGridViewColumn.ReadOnly = false;		
			//
			// uxUpdateByDataGridViewColumn
			//
			this.uxUpdateByDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxUpdateByDataGridViewColumn.DataPropertyName = "UpdateBy";
			this.uxUpdateByDataGridViewColumn.HeaderText = "UpdateBy";
			this.uxUpdateByDataGridViewColumn.Name = "uxUpdateByDataGridViewColumn";
			this.uxUpdateByDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxUpdateByDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxUpdateByDataGridViewColumn.ReadOnly = false;		
			//
			// uxIsActiveDataGridViewColumn
			//
			this.uxIsActiveDataGridViewColumn.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.DisplayedCells;
			this.uxIsActiveDataGridViewColumn.DataPropertyName = "IsActive";
			this.uxIsActiveDataGridViewColumn.HeaderText = "IsActive";
			this.uxIsActiveDataGridViewColumn.Name = "uxIsActiveDataGridViewColumn";
			this.uxIsActiveDataGridViewColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
			this.uxIsActiveDataGridViewColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic;
			this.uxIsActiveDataGridViewColumn.ReadOnly = false;		
			//
			// uxLoaiDaiLyIdDataGridViewColumn
			//				
			this.uxLoaiDaiLyIdDataGridViewColumn.DisplayMember = "TenLoaiKhachHang";	
			this.uxLoaiDaiLyIdDataGridViewColumn.ValueMember = "LoaiKhachHangId";	
			this.uxLoaiDaiLyIdDataGridViewColumn.DisplayStyleForCurrentCellOnly = true;
			//uxLoaiDaiLyIdDataGridViewColumn.DataSource = uxLoaiDaiLyIdBindingSource;				
				
			
			// this
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.Controls.Add(this.uxDaiLyDataGridView);
			this.Name = "DaiLyDataGridView";
			this.Size = new System.Drawing.Size(470, 300);
			//((System.ComponentModel.ISupportInitialize)(this.uxLoaiDaiLyIdBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDaiLyErrorProvider)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDaiLyDataGridView)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.uxDaiLyBindingSource)).EndInit();
			this.ResumeLayout(false);
		}
		#endregion
				
		#region events
		
		/// <summary>
		/// Raised the CurrentEntityChanged event.
		/// </summary>
		protected void OnCurrentEntityChanged()
		{
			if (CurrentEntityChanged != null)
			{
				DaiLyDataGridViewEventArgs args = new DaiLyDataGridViewEventArgs();
				args.DaiLy = _currentDaiLy;
				CurrentEntityChanged(this, args); 
			}
		}
		
		#endregion
		
		/// <summary>
		/// Strongly typed event 
		/// </summary>
		public class DaiLyDataGridViewEventArgs : System.EventArgs
		{
			private Entities.DaiLy	_DaiLy;
	
			/// <summary>
			/// the  Entities.DaiLy instance.
			/// </summary>
			public Entities.DaiLy DaiLy
			{
				get { return _DaiLy; }
				set { _DaiLy = value; }
			}	
		}
		
		/// <summary>
		/// Handles the DataError event of the uxDaiLyDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewDataErrorEventArgs"/> instance containing the event data.</param>
		protected void OnDaiLyDataGridViewDataError(object sender, System.Windows.Forms.DataGridViewDataErrorEventArgs e)
		{
			System.Diagnostics.Debug.WriteLine(string.Format( "{0}, {1}", e.ColumnIndex, e.RowIndex));
		}
		
		/// <summary>
		/// Handles the CellValueNeeded event of the uxDaiLyDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnDaiLyDataGridViewCellValueNeeded(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxDaiLyDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxDaiLyIdDataGridViewColumn":
						e.Value = DaiLyList[e.RowIndex].DaiLyId;
						break;
					case "uxTenDaiLyDataGridViewColumn":
						e.Value = DaiLyList[e.RowIndex].TenDaiLy;
						break;
					case "uxLoaiDaiLyIdDataGridViewColumn":
						e.Value = DaiLyList[e.RowIndex].LoaiDaiLyId;
						break;
					case "uxParentIdDataGridViewColumn":
						e.Value = DaiLyList[e.RowIndex].ParentId;
						break;
					case "uxLastUpdateDataGridViewColumn":
						e.Value = DaiLyList[e.RowIndex].LastUpdate;
						break;
					case "uxUpdateByDataGridViewColumn":
						e.Value = DaiLyList[e.RowIndex].UpdateBy;
						break;
					case "uxIsActiveDataGridViewColumn":
						e.Value = DaiLyList[e.RowIndex].IsActive;
						break;
				default:
				break;
			}
		}
		
		/// <summary>
		/// Handles the CellValuePushed event of the uxDaiLyDataGridView control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="T:System.Windows.Forms.DataGridViewCellValueEventArgs"/> instance containing the event data.</param>
		protected void OnDaiLyDataGridViewCellValuePushed(object sender, System.Windows.Forms.DataGridViewCellValueEventArgs e)
		{
			switch(uxDaiLyDataGridView.Columns[e.ColumnIndex].Name)
			{
					case "uxDaiLyIdDataGridViewColumn":
						DaiLyList[e.RowIndex].DaiLyId = (System.Int32)e.Value;
						break;
					case "uxTenDaiLyDataGridViewColumn":
						DaiLyList[e.RowIndex].TenDaiLy = (System.String)e.Value;
						break;
					case "uxLoaiDaiLyIdDataGridViewColumn":
						DaiLyList[e.RowIndex].LoaiDaiLyId = (System.Byte)e.Value;
						break;
					case "uxParentIdDataGridViewColumn":
						DaiLyList[e.RowIndex].ParentId = (System.Int32)e.Value;
						break;
					case "uxLastUpdateDataGridViewColumn":
						DaiLyList[e.RowIndex].LastUpdate = (System.DateTime)e.Value;
						break;
					case "uxUpdateByDataGridViewColumn":
						DaiLyList[e.RowIndex].UpdateBy = (System.Int32)e.Value;
						break;
					case "uxIsActiveDataGridViewColumn":
						DaiLyList[e.RowIndex].IsActive = (System.Boolean)e.Value;
						break;
				default:
				break;
			}
		}
	}
}
